package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Api(tags = "c端购物车相关接口")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @ApiOperation(value = "添加购物车")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车的商品信息为：{}",shoppingCartDTO);
        shoppingCartService.saveShoppingCart(shoppingCartDTO);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "查看购物车")
    public Result<List<ShoppingCart>> list(){
        log.info("用户查看购物车");
        List<ShoppingCart> result = shoppingCartService.list();
        return Result.success(result);
    }

    @DeleteMapping("/clean")
    @ApiOperation(value = "清空购物车")
    public Result delete(){
        log.info("用户想清空购物车所有物品");
        shoppingCartService.deleteAll();
        return Result.success();
    }

    @PostMapping("/sub")
    @ApiOperation(value = "删除购物车的一个物品")
    public Result deleteOne(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("用户想要删除的物品id为：{}",shoppingCartDTO);
        shoppingCartService.deleteSome(shoppingCartDTO);
        return  Result.success();

    }
}
